<template>
  <view class="container">
    <!-- 搜索 -->
    <view class="header">
      <image src="../../static/icon/search.png" mode="" style="position: absolute; left: 50rpx; width: 28rpx; height: 28rpx"></image>
      <input type="text" placeholder="搜索文章" v-model="title" @blur="search" class="ipt" placeholder-class="txtwhite" />
    </view>
    <!-- 文章 -->
    <scroll-view id="scrollHeight" scroll-y="true" style="width: 100%" :style="{ height: scrollheight * 2 + 'rpx' }" @scrolltolower="more">
      <view class="articlebox" v-for="item in list" :key="item.id" @click="goarticledetail(item.id)">
        <image :src="imgUrl + item.pic" v-if="item.pic != ''" mode="aspectFill" style="width: 710rpx; height: 280rpx"></image>
        <view style="font-size: 28rpx; font-family: PingFang SC, PingFang SC; font-weight: bold; color: #222222; padding-left: 30rpx; margin-top: 10rpx">
          {{ item.title }}
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      page: 1,
      list: [],
      imgUrl: '',
      title: '',
      currentid: '',
      scrollheight: ''
    };
  },
  methods: {
    // 获取数据列表
    async gtelist(id) {
      const res = await uni.$http.post('/news/articleList', {
        type: 'teacher',
        page: this.page,
        title: this.title,
        category_id: id
      });
      if (res.data.data.list.length != 0) {
        this.list.push(...res.data.data.list);
      } else {
        uni.showToast({
          title: '数据已加载完毕',
          icon: 'none'
        });
      }
    },
    search() {
      this.list = [];
      this.page = 1;
      this.gtelist(this.currentid);
    },
    more() {
      this.page++;
      this.gtelist(this.currentid);
    },
    // 跳转到文章详情
    goarticledetail(id) {
      uni.navigateTo({
        url: '/parents/articledetail/articledetail?id=' + id
      });
    }
  },
  onLoad(e) {
    this.imgUrl = this.imgurl;
    console.log('传递的id', e.id);
    if (e.id) {
      this.currentid = e.id;
      this.gtelist(e.id);
    }
  },
  onShow() {
    // 获取可用的高度
    uni.getSystemInfo({
      success: (res) => {
        const query = uni.createSelectorQuery();
        query
          .select('#scrollHeight')
          .boundingClientRect((data) => {
            this.scrollheight = res.windowHeight - data.top;
            console.log('可用的高度-----', this.scrollheight);
          })
          .exec();
      }
    });
  }
};
</script>

<style lang="scss" scoped>
.ipt::placeholder {
  color: #fff;
}
.container {
  box-sizing: border-box;
  .header {
    display: flex;
    align-items: center;
    width: 100%;
    height: 100rpx;
    background-color: #3553e8;
    .ipt {
      box-sizing: border-box;
      width: 692rpx;
      height: 64rpx;
      background: #5d75ed;
      padding-left: 80rpx;
      margin: 0 30rpx 0 28rpx;
      color: #fff;
      border-radius: 100rpx 100rpx 100rpx 100rpx;
    }
  }
  .articlebox {
    box-sizing: border-box;
    width: 710rpx;
    height: 360rpx;
    margin: 30rpx 20rpx;
    background: #ffffff;
    box-shadow: 0rpx 4rpx 4rpx 2rpx rgba(191, 191, 191, 0.16);
    border-radius: 20rpx 20rpx 20rpx 20rpx;
  }
  /deep/ .txtwhite {
    color: #fff !important;
  }
}
</style>
<style>
page {
  background-color: #f8f8f8;
}
</style>
